﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.Security;

namespace DomMgmtSystem
{
    public class CommonCode
    {
        public const string DEFAULTPWD = "123456";
        public static Model.TB_USER CurUser
        {
            get
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    string userID = HttpContext.Current.User.Identity.Name;
                    return DataAccess.TB_USER.GetModel(userID);
                }
                return null;
            }
        }
    }

    public static class Extens
    {
        /// <summary>
        /// jAlert提示信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg">提示内容</param>
        public static void Alert(this Page page, string msg)
        {
            string myScript = "<script defer=\"defer\">alert(\"" + msg + "\");</script> ";
            page.RegisterStartupScript("myScript", myScript);
        }
        /// <summary>
        /// jAlert提示信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg">提示内容</param>
        public static void Refresh(this Page page)
        {
            string myScript = "document.execCommand('Refresh'); ";
            page.ClientScript.RegisterStartupScript(page.ClientScript.GetType(), "myScript", myScript, true);
        }
        /// <summary>
        /// 提示信息之后跳转到另一页之前
        /// </summary>
        /// <param name="message">跳转之前的提示信息</param>
        /// <param name="redirectUrl">跳转到的页面URL</param>
        public static void RedirectAfterInfo(this Page page, string message, string redirectUrl)
        {
            HttpContext.Current.Response.Write("<script language='javascript'>alert('" + message + "');window.location='" + redirectUrl + "';</script>");
        }
        /// <summary>
        /// 提示信息之后跳转到另一页之前
        /// </summary>
        /// <param name="message">跳转之前的提示信息</param>
        /// <param name="CancelUrl">点取消跳转地址</param>
        /// <param name="OKUrl">点确定跳转地址</param>
        public static void RedirectConfirmInfo(this Page page, string message, string OKUrl, string CancelUrl)
        {
            HttpContext.Current.Response.Write("<script language='javascript'>if(confirm('" + message + "')){window.location='" + OKUrl + "';}else{window.location='" + CancelUrl + "';}</script>");
        }

        /// <summary>
        /// 提示信息之后跳转到另一页之前
        /// </summary>
        /// <param name="message">跳转之前的提示信息</param>
        /// <param name="redirectUrl">跳转到的页面URL</param>
        public static void LoginOut(this Page page)
        {
            HttpContext.Current.Response.Write("<script language='javascript'>parent.reLogin();</script>");
        }

        /// <summary>
        /// 递归查找找指定ID的控件
        /// </summary>
        /// <param name="Parent">父控件(控件容器)</param>
        /// <param name="id">要查找的控件ID</param>
        /// <returns>回传符合ID的控件，若未找到则返回Null</returns>
        public static System.Web.UI.Control FindControlEx(this System.Web.UI.Control parentControl, string id)
        {
            System.Web.UI.Control control = null;
            //先使用 FindControl 去查找指定的子控件
            control = parentControl.FindControl(id);
            //如果未找到则往下层递归方式去查找
            if (control == null)
            {
                foreach (System.Web.UI.Control oChildCtrl in parentControl.Controls)
                {
                    //以递归方式回调原函数
                    control = FindControlEx(oChildCtrl, id);
                    //如果找到指定控件则退出循环
                    if (control != null) break;
                }
            }
            return control;
        }

        /// <summary>
        /// 根据QueryString Name返回Request值
        /// </summary>
        /// <param name="queryStringName">QueryString Name</param>
        /// <returns>Request值</returns>
        public static string GetRequestStr(this Page page, string queryStringName)
        {
            if (page.Request[queryStringName] == null)
            {
                return "";
            }
            else
            {
                return page.Request[queryStringName].ToString().Replace("%20", "+");
            }
        }

        /// <summary>
        /// 硬盘数据类型
        /// </summary>
        public static Dictionary<string, string> HDDataType
        {
            get
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("CHD", "DOM正射影像五万生产");
                dic.Add("CDJ", "DOM正射影像五万收集");
                dic.Add("CSM", "DOM正射影像扫描数据");
                dic.Add("CTD", "DOM正射影像土地覆盖数据");
                dic.Add("CGW", "长城测量");
                dic.Add("CD2", "原始卫星影像");
                dic.Add("CDOM", "成果数据");
                dic.Add("CLSHP", "历史航片数据");
                dic.Add("CYD", "移动通讯数据");
                dic.Add("CRS", "对外提供数据");
                dic.Add("CQT", "其它数据");
                return dic;
            }
        }
        /// <summary>
        /// 根据字典生成下拉列表的选项
        /// </summary>
        /// <param name="zdName">字典里对应的名称</param>
        /// <param name="dt">字典Table</param>
        /// <returns>下拉列表的Options字符串</returns>
        public static string GetSelectString(this DataTable dt, string zdName, string defaultValue)
        {
            string option = "<option value='{0}'{2}>{1}</option>";
            string options = "";
            var query = from DataRow p in dt.Rows
                        where p["ZD_NAME"].ToString() == zdName
                        select p;
            if (query.Count() > 0)
            {
                foreach (var item in query)
                {
                    if (item["ZD_CODE"].ToString() == defaultValue)
                        options += string.Format(option, item["ZD_CODE"].ToString(), item["ZD_TYPE"].ToString(), " selected='selected'");
                    else
                        options += string.Format(option, item["ZD_CODE"].ToString(), item["ZD_TYPE"].ToString(), "");
                }
            }
            else
                return "";
            return options;
        }
    }
}